package com.zk.curator.lock;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;

/**
 * <h3>partitionjob</h3>
 * <p>java锁</p>
 *
 * @author : hduong
 * @version : 1.0
 * @date : 2019-12-24 09:38
 **/
public class Lock1 {
    static ReentrantLock lock = new ReentrantLock();
    static int count = 10;
    public static void genarNo() {
        try {
            lock.lock();
            TimeUnit.SECONDS.sleep(1);
            System.out.println( "count : " + count--);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    public static void main(String[] args) throws InterruptedException {
        final CountDownLatch countDownLatch = new CountDownLatch(1);
        for (int i = 0; i < 10; i ++) {
            new Thread(() -> {
                try {
                    //阻塞在这个地方 ： 如果计数器没有减少 会一直阻塞在这个位置
                    countDownLatch.await();
                    genarNo();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }, "线程-" + i).start();
        }
        TimeUnit.SECONDS.sleep(1);
        countDownLatch.countDown();

    }
}
